﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data;
using System.Data.Common;

namespace NearForums.DataAccess
{
    public class AskQuestionDataAccess:BaseDataAccess, IAskQuestionDataAccess
    {
        /// <summary>
        /// </summary>
        /// <param name="pageSize"></param>
        /// <param name="pageNum"></param>
        /// <returns></returns>
        public List<Questions> GetAllQuestionsByPage(int pageSize, int pageNum)
        {
            DbCommand comm = GetCommand("Paging_RowCount");
            comm.AddParameter(this.Factory, "Tables", DbType.String, "V_QuestionComplete");
            comm.AddParameter(this.Factory, "PK", DbType.String, "q_ID");
            comm.AddParameter(this.Factory, "Filter", DbType.String, "isActive = 1");
            comm.AddParameter(this.Factory, "PageSize", DbType.Int32, pageSize); 
            comm.AddParameter(this.Factory, "PageNumber", DbType.Int32,  pageNum);

            DataTable dt = GetTable(comm);
            List<Questions> questionList = new List<Questions>();

            foreach (DataRow dr in dt.Rows)
            {
                Questions question = ParseQuestionByDataRow(dr);
                questionList.Add(question);
            }

            return questionList;
        }

        public void Add(Questions q, int forumId, int userID)
        {
            DbCommand comm = this.GetCommand("SPQuestionInsert");
            comm.AddParameter(this.Factory, "name", DbType.String, q.Name);
            comm.AddParameter(this.Factory, "title", DbType.String, q.Title);
            comm.AddParameter(this.Factory, "description", DbType.String, q.Description);
            comm.AddParameter(this.Factory, "forumID", DbType.Int32, forumId);
            comm.AddParameter(this.Factory, "userID", DbType.Int32,userID);

            comm.SafeExecuteNonQuery();
        }

        public void Update(Questions q, int forumId)
        {
            DbCommand comm = this.GetCommand("SPQuestionInsert");
            comm.AddParameter(this.Factory, "q_ID", DbType.String, q.Id);
            comm.AddParameter(this.Factory, "title", DbType.String, q);
            comm.AddParameter(this.Factory, "description", DbType.String, q.Description);
            comm.AddParameter(this.Factory, "forumID", DbType.Int32, forumId);

            comm.SafeExecuteNonQuery();
        }

        public void Delete(int questionID)
        {
            DbCommand comm = GetCommand("SPQuestionDelete");
            comm.AddParameter(this.Factory, "questionID", DbType.String, questionID);

            comm.SafeExecuteNonQuery();
        }

        public List<Questions> GetReletedQuestion(string keyword, Questions q)
        {
            throw new NotImplementedException();
        }

        public Questions GetQuestionById(int id)
        {
            DbCommand comm = GetCommand("SPQuestionGetById");
            comm.AddParameter(this.Factory, "id", DbType.Int32, id);
            DataRow questionR = GetFirstRow(comm);
            return ParseQuestionByDataRow(questionR);
        }

        public void AddVisit(int questionId)
        {
            DbCommand comm = GetCommand("SPQuestionAddVisit");
            comm.AddParameter(this.Factory, "questionID", DbType.Int32, questionId);
            comm.SafeExecuteNonQuery();
        }

        protected virtual Questions ParseQuestionByDataRow(DataRow dr)
        {
            Questions q = new Questions();
            q.Id = dr.Get<int>("q_ID");
            q.Name = dr.GetString("name");
            q.PublishDate = dr.GetDate("publishDate");
            q.Title = dr.GetString("title");
            q.Description = dr.GetString("description");
            q.Forum = new Forum();
            q.Forum.Id = dr.Get<int>("forumID");
            q.User = new User(dr.Get<int>("userID"),dr.GetString("UserName"));
            q.User.Photo = dr.GetString("UserPhoto");
            q.ViewCount = dr.Get<int>("viewed");

            return q;
        }
    }
}
